<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#assign currentMenu="strategyDetail"/>
    <#include "../common/header.ftl">
    <script src="/js/plugins/jquery-bootstrap/jquery.bootstrap.min.js"></script>
    <script src="/js/plugins/jquery-form/jquery.form.js"></script>
    <style type="text/css">
        .modal-body {
            max-height: 750px;
            overflow-y: auto;
        }
        .modal-body img {
            width: 100%;
        }
    </style>

</head>
<body>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-lg-3">
            <#include "../common/menu.ftl">
        </div>
        <div class="col-lg-9">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-head-line">攻略文章管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/strategyDetail/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <label>关键字:</label>
                    <input type="text" class="form-control" name="keyword" value="${(qo.keyword)!}"
                           placeholder="请输入标题和攻略类别">
                </div>
                <button class="btn btn-info">
                    <span class="glyphicon glyphicon-search"></span> 查询
                </button>
                <a href="javascript:" class="btn btn-success inputBtn">
                    <span class="glyphicon glyphicon-plus"></span> 新增
                </a>
            </form>
            <table class="table table-striped table-hover">
                <tr>
                    <th>序号</th>
                    <th>标题</th>
                    <th>封面</th>
                    <th>发布时间</th>
                    <th>排序</th>
                    <th>攻略类别</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
                <#list pageInfo.list as entity>
                    <tr>
                        <td>${entity_index + 1}</td>
                        <td>${entity.title}</td>
                        <td><img src="${entity.coverUrl}" width="100"/></td>
                        <td>${(entity.releaseTime?string("yyyy-MM-dd HH:mm"))!}</td>
                        <td>${entity.sequence}</td>
                        <td>${entity.catalog.name}</td>
                        <td>${entity.stateName}</td>
                        <td>
                            <a role="button" class="btn btn-primary btn-xs inputBtn" data-json='${entity.json}'>
                                <span class="glyphicon glyphicon-sunglasses"></span> 修改
                            </a>
                        </td>
                    </tr>
                </#list>
            </table>
            <#include "../common/page.ftl">
        </div>
    </div>
</div>
<#-- strategyDetail -->
<div id="inputModal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/strategyDetail/saveOrUpdate.do" method="post"
                      enctype="multipart/form-data" id="editForm">
                    <input type="hidden" name="id"/>
                    <div class="form-group" >
                        <label class="col-lg-4 control-label">标题：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="title"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">封面：</label>
                        <div class="col-lg-6">
                            <img id="coverUrl" style="width: 100%"/>
                            <input type="file" class="form-control" name="img"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">所属攻略：</label>
                        <div class="col-lg-6">
                            <select class="form-control" id="strategy">
                                <#list strategys as s>
                                    <option value="${s.id}">${s.title}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">攻略类别：</label>
                        <div class="col-lg-6">
                            <select class="form-control" name="catalog.id" id="catalog">
                            </select>
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="col-lg-4 control-label">排序：</label>
                        <div class="col-lg-6">
                            <input type="number" class="form-control" name="sequence"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">状态：</label>
                        <div class="col-lg-6">
                            <select class="form-control" name="state">
                                <option value="0">发布</option>
                                <option value="1">草稿</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <textarea name="strategyContent.content" id="ckeditor"></textarea>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary submitBtn">保存</button>
            </div>
        </div>
    </div>
</div>
<script src="/js/plugins/ckeditor/ckeditor.js"></script>
<script type="text/javascript">
    $(function () {
        //定义一个全局的变量去接收当前攻略文章中的攻略分类的对象的id
        var catalogId ;

        //给保存按钮和添加按钮绑定点击事件
        $(".inputBtn").click(function () {
            //先清除模态框中的内容
            $("#editForm input").val("");
            $("#coverUrl").attr("src","");
            edt.setData('');
            //拿到存储起来的数据
            var json = $(this).data("json");
            if(json){
                $.get("/strategyDetail/getContent.do?id="+json.id,function (data) {
                    edt.setData(data);//渲染文本框中的内容
                })
                //开始渲染数据
                $("#editForm input[name='id']").val(json.id);
                $("#editForm input[name='title']").val(json.title);
                $("#coverUrl").attr("src",json.coverUrl);//背景图片
                $("#strategy").val(json.strategyId);//回显一级的数据
                $("#editForm input[name='sequence']").val(json.sequence);
                $("#editForm select[name='state']").val(json.state);
                catalogId=json.catalogId;//将事先藏好的攻略分类的id赋给全局变量
            }else{
                //如果没有Json的话就将这个值清空掉
                catalogId='';
            }
            //当我点击按钮的时候，就拿当前一级下拉列表中选中的那个去查询二级的数据
            linkage($("#strategy option:selected").val(),catalogId);
            //打开模态窗
            setTimeout(function () {
                $("#inputModal").modal("show");
            },20);
        })

        //给大攻略这个下拉框绑定值改变事件
        $("#strategy").change(function () {
            //拿到当前的value值去发送get请求，获取别的攻略分类
            linkage($("#strategy option:selected").val(),catalogId);
        })

        //初始化一个CKEDITOR
        var edt = CKEDITOR.replace("ckeditor");

        //给提交按钮绑定事件
        $(".submitBtn").click(function () {
            //将富文本框中的数据赋值到表单提交参数上面
            $("#ckeditor").html(edt.getData());

            $("#editForm").ajaxSubmit(function (data) {
                if(data.success){
                    window.location.reload();
                }else{
                    alert(data.msg);
                }
            })
        })



    })
    function linkage(strategyId,catalogId) {
        $("#catalog").html("");//清空攻略分类的下拉列表
        //拿到攻略的id，发送get请求，获取所属攻略分类的的信息
        $.get("/strategyCatalog/queryCatalogBystrategyId.do?strategyId="+strategyId,function (data) {
            //循环遍历数据，拼接到攻略类别的下拉列表中
            $.each(data,function (index, ele) {
                $("<option></option>").val(ele.id).html(ele.name).appendTo("#catalog");
            })

            //在数据拼接成功之后就开始回显二级的数据
            if(catalogId){
                $("#catalog").val(catalogId);
            }
        })
    }
</script>
</body>
</html>